const dbInit = require('../config/db')
const User = require('../model/UserModel')
const Role = require('../model/RoleModel')
const Api = require('../model/ApiModel')
const Menu = require('../model/MenuModel')
const restoreData = require('./data')
dbInit()
const fs = require('fs')

const storeData = (data, path) => {
  try {
    fs.writeFileSync(path, JSON.stringify(data))
  } catch (err) {
    console.error(err)
  }
}

const loadData = (path) => {
  try {
    return fs.readFileSync(path, 'utf8')
  } catch (err) {
    console.error(err)
    return false
  }
}
// eslint-disable-next-line no-unused-expressions
!(async () => {
  // const users = await User.find()
  // const roles = await Role.find()
  // const menus = await Menu.find()
  // const apis = await Api.find()
  // const data = { users, roles, menus, apis }
  // storeData(data, './data.json')
  const { users, roles, menus, apis } = restoreData
  await User.deleteMany()
  await Menu.deleteMany()
  await Role.deleteMany()
  await Api.deleteMany()
  await Api.create(apis)
  await Menu.create(menus)
  await Role.create(roles)
  await User.create(users)
  console.log('============> done')
})()
